package hk.linkcredit.walletservice.wallet.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hk.linkcredit.walletservice.constant.WalletConstants;
import hk.linkcredit.walletservice.wallet.domain.entity.WalletFlow;
import hk.linkcredit.walletservice.wallet.domain.entity.WalletOrder;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletAccountInfo;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletAccountReq;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletExpend;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletIncome;
import hk.linkcredit.walletservice.wallet.mapper.WalletFlowMapper;
import hk.linkcredit.walletservice.wallet.service.WalletFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 钱包流水表 服务实现类
 * </p>
 *
 * @author LinkCredit
 * @since 2022-06-10 15:18:10
 */
@Slf4j
@Service
public class WalletFlowServiceImpl extends ServiceImpl<WalletFlowMapper, WalletFlow> implements WalletFlowService {

    @Autowired
    WalletFlowMapper walletFlowMapper;

    @Override
    public List<WalletIncome> getWalletIncome(String custNo, String flowType) {
        return walletFlowMapper.getWalletIncome(custNo, flowType);
    }

    @Override
    public List<WalletExpend> getWalletExpend(String custNo) {
        return walletFlowMapper.getWalletExpend(custNo);
    }

    @Override
    public void createWalletFlowExpend(WalletOrder walletOrder) {
        log.info("开始创建支出流水 >>> orderType = {}, walletOrder = {}", walletOrder.getOrderType(), walletOrder);

        List<WalletFlow> walletFlowList = new ArrayList<>();
        String orderType = walletOrder.getOrderType();

        WalletFlow walletFlow = WalletFlow.builder()
                .custNo(walletOrder.getCustNo())
                .walletNo(walletOrder.getWalletNo())
                .walletType(walletOrder.getWalletType())
                .changeAmount(walletOrder.getAmount())
                .changeType(WalletConstants.WALLET_FLOW_CHANGE_TYPE_EXPEND)
                .fundsType(orderType)
                .loanSerialNo(walletOrder.getLoanSerialNo())
                .orderSerialNo(walletOrder.getSerialNo())
                .fromWhere(walletOrder.getFromWhere())
                .toDest(walletOrder.getToDest())
                .createTime(new Date())
                .build();

        walletFlowList.add(walletFlow);

        if (walletOrder.getServiceFee().compareTo(BigDecimal.ZERO) > 0) {
            WalletFlow walletFlowServiceFee = WalletFlow.builder()
                    .custNo(walletOrder.getCustNo())
                    .walletNo(walletOrder.getWalletNo())
                    .walletType(walletOrder.getWalletType())
                    .changeAmount(walletOrder.getServiceFee())
                    .changeType(WalletConstants.WALLET_FLOW_CHANGE_TYPE_EXPEND)
                    .fundsType(WalletConstants.WALLET_FLOW_FUNDS_TYPE_SERVICE_FEE)
                    .loanSerialNo(walletOrder.getLoanSerialNo())
                    .orderSerialNo(walletOrder.getSerialNo())
                    .fromWhere(walletOrder.getFromWhere())
                    .toDest(WalletConstants.WALLET_FLOW_TO_ADMIN)
                    .createTime(new Date())
                    .build();
            walletFlowList.add(walletFlowServiceFee);
        }

        this.saveBatch(walletFlowList);

        log.info("完成创建支出流水 >>>");
    }

    @Override
    public void createWalletFlowIncome(String walletType, WalletAccountReq walletAccountReq, WalletAccountInfo walletAccountInfo) {
        log.info("开始创建收入流水 >>> walletType = {}, walletAccountReq = {}，walletAccountInfo = {}", walletType, walletAccountReq, walletAccountInfo);

        WalletFlow walletFlow = WalletFlow.builder()
                .custNo(walletAccountInfo.getCustNo())
                .walletNo(walletAccountInfo.getWalletNo())
                .walletType(walletType)
                .changeAmount(walletAccountReq.getAmount())
                .changeType(WalletConstants.WALLET_FLOW_CHANGE_TYPE_INCOME)
                .fundsType(walletAccountReq.getFundsType())
                .fromWhere(WalletConstants.WALLET_FLOW_TO_ADMIN)
                .toDest(walletAccountInfo.getWalletNo())
                .reference(walletAccountReq.getReference())
                .payTime(walletAccountReq.getPayTime())
                .msgId(walletAccountReq.getMsgId())
                .createTime(new Date())
                .build();

        this.save(walletFlow);

        log.info("完成创建收入流水 >>>");
    }

    @Override
    public BigDecimal getAmountSumByFoundType(String custNo, String flowType) {
        return baseMapper.getSumAmountByFoundsType(custNo, flowType);
    }

}
